﻿using System;
using System.Collections.Generic;
using System.IO;
using xycn.pluginsupport.BaseHelperPlugins;
using xycn.pluginsupport.ControllerPlugins;

namespace SampleController
{
    /// <summary>
    /// 示例控制器
    /// </summary>
    public class SampleController : BaseController
    {
        /// <summary>
        /// 默认Get操作
        /// </summary>
        /// <returns></returns>
        public SampleResult Get()
        {
            SampleResult ret = new SampleResult();
            ret.test1 = "SampleController:Get不带任何属性";

            string ip = base.controllerContext.localIP;

            base.controllerContext.responseInfo.newHeaders = new List<KeyValuePair<string, string>>();
            base.controllerContext.responseInfo.newHeaders.Add(new KeyValuePair<string,string>("lx","123123"));
            base.controllerContext.responseInfo.newHeaders.Add(new KeyValuePair<string, string>("lx", "123123333"));

            base.controllerContext.responseInfo.newCookies = new List<KeyValuePair<string, string>>();
            base.controllerContext.responseInfo.newCookies.Add(new KeyValuePair<string, string>("lx", "123123"));
            base.controllerContext.responseInfo.newCookies.Add(new KeyValuePair<string, string>("lx", "123123333"));

            base.controllerContext.responseInfo.redirectLocation = "http://www.baidu.com";
            return ret;
        }

        [ActionMethod]
        public string GetAction()
        {
            return "GetAction";
        }

        

        [HttpMethod(HttpMethodType.GET, "/test1/{text}/dfasd")]
        public SampleResult GetWithAbsolutePath(string text)
        {
            SampleResult ret = new SampleResult();
            ret.test1 = "SampleController:Get 方法属性指定路由（绝对路径）:" + text;
            return ret;
        }

        // GET api/values/5
        [HttpMethod(HttpMethodType.GET, "[controller]/{id}/{test}/{y}/{cc}")]
        public SampleResult GetWithMultiParameter(int id, int test, string y)

        {
            SampleResult ret = new SampleResult();
            ret.test1 = "SampleController:Get 方法属性指定路由（相对路径）:" + y;
            return ret;
        }

        [HttpMethod(HttpMethodType.GET,"[controller]/{id}/{r}")]
        public void Gettt(int id, [FromQuery] int test, 
            [FromBody] string y, [FromHeader] string Accept,
            [FromForm] string ttt, [FromRoute] string r)
        {
            SampleResult ret = new SampleResult();
            ret.test1 = "SampleController:测试多来源输入";
            return;
        }


        [HttpMethod(HttpMethodType.POST, "{id}")]
        public void Post(int id, [FromBody]SampleResult result)
        {
            Console.WriteLine(id.ToString());
            Console.WriteLine(result.test1);
            Console.WriteLine(result.subClass2s.Count.ToString());
        }

        [HttpMethod(HttpMethodType.POST, "file/{id}")]
        public void Post(int id, [FromBody]Stream stream)
        {
            using (FileStream file = new FileStream("d:\\ttt"+id.ToString() + ".txt", FileMode.Create))
            {
                byte[] buffer = new byte[1024 * 1024];
                
                int start_pos = 0;
                for (int i = 0; ; i++) {
                    int read_count = stream.Read(buffer, 0, 1024 * 1024);
                    if (read_count <= 0)
                        break;

                    file.Write(buffer, 0, read_count);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="stream"></param>
        [HttpMethod(HttpMethodType.POST, "files")]
        public void PostFiles(int id, [FromForm]UploadFiles upload_files,[FromQuery]string jjj)
        {
            foreach (UploadFile uploadfile in upload_files.uploadFiles)
            {
                using (FileStream file = new FileStream("d:\\" + uploadfile.fileName, FileMode.Create))
                {
                    byte[] buffer = new byte[1024 * 1024];

                    int start_pos = 0;
                    for (int i = 0; ; i++)
                    {
                        int read_count = uploadfile.fileReaderStream.Read(buffer, 0, 1024 * 1024);
                        if (read_count <= 0)
                            break;

                        file.Write(buffer, 0, read_count);
                    }
                }
                uploadfile.fileReaderStream.Dispose();
            }
        }

        [HttpMethod(HttpMethodType.GET, "/file/{filename}")]
        public Stream GetFile()
        {
            System.IO.FileStream file = new System.IO.FileStream("d:\\ttt.txt",
                FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            return file;
        }

        [HttpMethod(HttpMethodType.GET, "/download/{file_name}")]
        public DownloadFile DownloadFile()
        {
            DownloadFile ret = new DownloadFile();

            IFileHelper.IFileHelper fileHelper = 
                FileHelper.CreateFileService("LocalFileHelper");

            Stream file = fileHelper.OpenRead("d:\\ttt.txt");

            //FileStream file = new FileStream("d:\\ttt.txt", FileMode.Open);

            ret.fileName = "ttt.txt";
            ret.fileLength = file.Length;
            ret.fileStream = file;


            return ret;
        }

        public void Post(int id, [FromQuery] int test, [FromQuery] string y, [FromQuery] string cc)
        {

        }

        [HttpMethod(HttpMethodType.PUT)]
        public void Put()
        {

        }
    }

    [ApiController]
    public class Sample1
    {
        public SampleResult Get()
        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample1:Get不带任何属性";
            return ret;
        }

        [HttpMethod(HttpMethodType.GET, "/test2/{text}")]
        public SampleResult GetWithAbsolutePath(string text)
        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample1:Get 方法属性指定路由（绝对路径）:" + text;
            return ret;
        }

        // GET api/values/5
        [HttpMethod(HttpMethodType.GET, "[controller]/{id}/{test}/{y}/{cc}")]
        public SampleResult GetWithMultiParameter(int id, int test, string y)

        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample1:Get 方法属性指定路由（相对路径）:" + y;
            return ret;
        }

        [HttpMethod(HttpMethodType.POST)]
        public void Post(int id, [FromQuery] int test, [FromQuery] string y, [FromQuery] string cc)
        {

        }

        [HttpMethod(HttpMethodType.PUT)]
        public void Put()
        {

        }
    }

    [ApiController]
    [Route("/v1/[controller]")]
    public class Sample3
    {
        public SampleResult Get()
        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample3:Get不带任何属性";
            return ret;
        }

        [HttpMethod(HttpMethodType.GET, "/test3/{text}")]
        public SampleResult GetWithAbsolutePath(string text)
        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample3:Get 方法属性指定路由（绝对路径）:" + text;
            return ret;
        }

        // GET api/values/5
        [HttpMethod(HttpMethodType.GET, "[controller]/{id}/{test}/{y}/{cc}")]
        public SampleResult GetWithMultiParameter(int id, int test, string y)

        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample3:Get 方法属性指定路由（相对路径）:" + y;
            return ret;
        }

        [HttpMethod(HttpMethodType.POST)]
        public void Post(int id, [FromQuery] int test, [FromQuery] string y, [FromQuery] string cc)
        {

        }

        [HttpMethod(HttpMethodType.PUT)]
        public void Put()
        {

        }
    }

    [ApiController]
    [Route("/v1/v2/")]
    [Route("/v1/v3/v4/v5")]
    public class Sample4
    {
        public SampleResult Get()
        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample4:Get不带任何属性,Controller多路由指定";
            return ret;
        }

        [HttpMethod(HttpMethodType.GET, "/test4/{text}")]
        public SampleResult GetWithAbsolutePath(string text)
        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample4:Get 方法属性指定路由（绝对路径）:" + text;
            return ret;
        }

        // GET api/values/5
        [HttpMethod(HttpMethodType.GET, "[controller]/{id}/{test}/{y}/{cc}")]
        public SampleResult GetWithMultiParameter(int id, int test, string y)

        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample4:Get 方法属性指定路由（相对路径）:" + y;
            return ret;
        }

        [HttpMethod(HttpMethodType.POST)]
        public void Post(int id, [FromQuery] int test, [FromQuery] string y, [FromQuery] string cc)
        {

        }

        [HttpMethod(HttpMethodType.PUT)]
        public void Put()
        {

        }
    }

    [ApiController]
    [Route("/v1/v2")]    
    public class Sample5
    {
        [Route("/v1/v3/v4/v6")]
        [Route("/v1/v3/v4/v7")]
        [Route("/v1/v3/v4/v8")]
        public SampleResult Get()
        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample5:Get不带任何属性,Controller多路由指定";
            return ret;
        }

        [HttpMethod(HttpMethodType.GET, "/test5/{text}")]
        public SampleResult Get(string text)
        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample5:Get 方法属性指定路由（绝对路径）:" + text;
            return ret;
        }

        // GET api/values/5
        [HttpMethod(HttpMethodType.GET, "[controller]/{id}/{test}/{y}/{cc}")]
        public SampleResult GetWithMultiParameter(int id, int test, string y)

        {
            SampleResult ret = new SampleResult();
            ret.test1 = "Sample5:Get 方法属性指定路由（相对路径）:" + y;
            return ret;
        }

        [HttpMethod(HttpMethodType.POST)]
        public void Post(int id, [FromQuery] int test, [FromQuery] string y, [FromQuery] string cc)
        {

        }

        [HttpMethod(HttpMethodType.PUT)]
        public void Put()
        {

        }
    }
}
